<template>
  <div class="wraper">
    <el-dialog
      class="add-dialog"
      title="新增试轴器"
      :visible.sync="addDialog.showDialog"
      width="60%"
    >
      <div class="input-row">
        <div class="input-desc">名称:</div>
        <el-input class="row-input" v-model="addDialog.name"></el-input>
      </div>
      <div class="input-row">
        <div class="input-desc">行数:</div>
        <el-input-number
          class="row-input"
          v-model="addDialog.row"
          :min="1"
          label="描述文字"
        ></el-input-number>
      </div>
      <div class="input-row">
        <div class="input-desc">列数:</div>
        <el-input-number
          class="col-input"
          v-model="addDialog.col"
          :min="1"
          label="描述文字"
        ></el-input-number>
      </div>
      <div class="input-row">
        <el-checkbox
          v-model="addDialog.open"
          label="是否公开"
          border
        ></el-checkbox>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialog.showDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitTester">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      class="resize-dialog"
      title="修改试轴器大小"
      :visible.sync="resizeDialog.showDialog"
      width="60%"
    >
      <div class="input-row">
        行数:
        <el-input-number
          v-model="resizeDialog.row"
          :min="1"
          label="描述文字"
        ></el-input-number>
      </div>
      列数:
      <el-input-number
        v-model="resizeDialog.col"
        :min="1"
        label="描述文字"
      ></el-input-number>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resizeDialog.showDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitResize">确 定</el-button>
      </span>
    </el-dialog>

    <div class="button-row">
      <el-button type="primary" @click="addDialog.showDialog = true"
        >新增试轴器</el-button
      >
    </div>
    <div class="search-row">
      <el-input placeholder="请输入内容" v-model="keyword" class="search-input">
        <el-button slot="append" icon="el-icon-search" @click="refreshList"></el-button>
      </el-input>
    </div>
    <el-table :data="list" style="width: 100%">
      <el-table-column label="序号" width="80" prop="index" />
      <el-table-column label="名称" width="80" prop="name" />
      <el-table-column label="公开情况" width="80" prop="openDesc" />
      <el-table-column label="试轴器孔位" width="100" prop="total" />
      <el-table-column label="试轴器类型" width="120" prop="total">
        <template slot-scope="scope">
          <span
            >{{ scope.row.rowCount }} * {{ scope.row.colCount
            }}{{ scope.row.typeName }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="创建日期" width="140" prop="createTime" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="editPlate(scope.$index, scope.row)"
            >编辑试轴器</el-button
          >
          <el-button
            size="mini"
            type="info"
            @click="viewPlate(scope.$index, scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteTester(scope.$index, scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="warning"
            @click="resize(scope.$index, scope.row)"
            >修改试轴器大小</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="pageTotal"
      :page-size="pageSize"
      @current-change="changePage"
    ></el-pagination>
  </div>
</template>
<script>
import {
  Button,
  Dialog,
  InputNumber,
  Input,
  Checkbox,
  Table,
  TableColumn,
  Pagination
} from "element-ui";
import { addTester } from "@/api/tester.js";
import {
  getAdminTesterList,
  deleteAdminTester,
  resizeAdminTester
} from "@/api/admin-tester.js";
export default {
  name: "tester",
  components: {
    [Button.name]: Button,
    [InputNumber.name]: InputNumber,
    [Dialog.name]: Dialog,
    [Input.name]: Input,
    [Checkbox.name]: Checkbox,
    [Table.name]: Table,
    [TableColumn.name]: TableColumn,
    [Pagination.name]: Pagination
  },
  data() {
    return {
      addDialog: {
        showDialog: false,
        row: 0,
        col: 0,
        name: null,
        open: false
      },
      resizeDialog: {
        showDialog: false,
        id: null,
        row: 0,
        col: 0
      },
      list: [],
      pageNum: 1,
      pageSize: 10,
      keyword: null,
      pageTotal: 0
    };
  },
  mounted() {
    this.refreshList();
  },
  methods: {
    refreshList() {
      getAdminTesterList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        keyword: this.keyword
      })
        .then(res => {
          this.list = res.list;
          this.list.forEach((item, index) => (item.index = index + 1));
          this.pageTotal = res.total;
        })
        .catch(errMsg => this.$alert(errMsg.msg));
    },
    changePage(page) {
      this.pageNum = page;
      this.refreshList();
    },
    submitTester() {
      addTester({
        name: this.addDialog.name,
        row: this.addDialog.row,
        col: this.addDialog.col,
        open: this.addDialog.open
      })
        .then(res => {
          this.refreshList();
          this.addDialog.showDialog = false;
        })
        .catch(errMsg => this.$alert(errMsg.msg));
    },
    deleteTester(index, row) {
      this.$confirm("此操作将永久删除该试轴器, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteAdminTester(row.id)
            .then(res => this.refreshList())
            .catch(errMsg => this.$alert(errMsg.msg));
        })
        .catch();
    },
    resize(index, row) {
      this.resizeDialog.showDialog = true;
      this.resizeDialog.id = row.id;
      this.resizeDialog.row = row.rowCount;
      this.resizeDialog.col = row.colCount;
    },
    submitResize() {
      resizeAdminTester({
        id: this.resizeDialog.id,
        row: this.resizeDialog.row,
        col: this.resizeDialog.col
      })
        .then(res => {
          this.refreshList();
          this.resizeDialog.showDialog = false;
        })
        .catch(errMsg => this.$alert(errMsg.msg));
    },
    editPlate(index, row) {
      this.$router.push({ path: `/backend/config-tester/${row.id}` });
    },
    viewPlate(index, row) {
      this.$router.push({ path: `/backend/view-tester/${row.id}` });
    }
  }
};
</script>
<style lang="stylus" scoped>
.wraper {
  .add-dialog {
    .input-row {
      margin-bottom: 5px;

      .input-desc {
        position: absolute;
        width: 50px;
        line-height: 40px;
      }

      .row-input {
        width: 200px;
        margin-left: 50px;
      }

      .col-input {
        width: 200px;
        margin-left: 50px;
      }
    }
  }

  .search-row {
    margin-top: 10px;

    .search-input {
      width: 400px;
    }
  }
}
</style>